import shop from '../../api/shop'

const state = {
    all: []
}

const getters = {
    allProducts: state => state.all
}

const actions = {
    getAllProducts({ commit }) {
        shop.getProducts(function (products) {
            commit('setProducts', products)
        })
    }
}

const mutations = {
    setProducts(state, products) {
        state.all = products
    },

    decrementProductInventory(state, payload) {
        const product = state.all.find(function (product) {
            return product.id === payload.id
        })
        product.inventory--
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}